#include "lcd_init.h"
#include "stdio.h"
#include <string.h>
// #include "app.h"

typedef void (*FunType)(void);
//**********************************write function begin********************************************
void wr_cmd_4spi_8bcs(uint8_t par)
{
    LCD_DCXLow();
    LCD_CSLow();

    LCD_SCLLow();
    if (par & 0x80)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x40)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x20)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x10)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x08)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x04)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x02)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x01)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_CSHigh();
    LCD_DCXHigh();
    HAL_Delay(5);
}
void wr_dat_4spi_8bcs(uint8_t par)
{
    LCD_DCXHigh();
    LCD_CSLow();

    LCD_SCLLow();
    if (par & 0x80)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x40)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x20)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x10)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x08)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x04)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x02)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x01)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_CSHigh();
    LCD_DCXHigh();
    HAL_Delay(1);
}
void wr_cmd_parall1_8(uint16_t par)
{
    LCD_DCXLow();
    LCD_CSLow();
    LCD_WRLow();
    GPIOB->ODR = par;

    LCD_WRHigh();
}
void wr_dat_parall1_8(uint16_t par)
{
    LCD_DCXHigh();
    LCD_WRLow();
    GPIOB->ODR = par;

    LCD_WRHigh();
}
void wr_cmd_parall2_8(uint16_t par)
{
    LCD_DCXLow();
    LCD_CSLow();
    LCD_WRLow();
    GPIOB->ODR = par << 1;

    LCD_WRHigh();
}
void wr_dat_parall2_8(uint16_t par)
{
    LCD_DCXHigh();
    LCD_WRLow();
    GPIOB->ODR = par << 1;

    LCD_WRHigh();
}

void wr_cmd_3spi_9bit(uint8_t par)
{
    LCD_CSLow();
    LCD_SCLLow();
    LCD_SDALow();
    LCD_SCLHigh();

    for (uint8_t i = 0; i < 8; i++)
    {
        LCD_SCLLow();
        if (par & 0x80)
        {
            LCD_SDAHigh();
        }
        else
        {
            LCD_SDALow();
        }
        par = par << 1;
        LCD_SCLHigh();
    }
    LCD_CSHigh();
}

void wr_dat_3spi_9bit(uint8_t par)
{
    LCD_CSLow();
    LCD_SCLLow();
    LCD_SDAHigh();
    LCD_SCLHigh();

    for (uint8_t i = 0; i < 8; i++)
    {
        LCD_SCLLow();
        if (par & 0x80)
        {
            LCD_SDAHigh();
        }
        else
        {
            LCD_SDALow();
        }
        par = par << 1;
        LCD_SCLHigh();
    }
    LCD_CSHigh();
}

void LCD_WR_REG(uint8_t par)
{
    LCD_SCLLow();
    LCD_DCXLow();
    LCD_CSLow();

    for (uint8_t i = 0; i < 8; i++)
    {
        LCD_SCLLow();
        if (par & 0x80)
        {
            LCD_SDAHigh();
        }
        else
        {
            LCD_SDALow();
        }
        par = par << 1;
        LCD_SCLHigh();
    }
    LCD_CSHigh();
}

void LCD_WR_DATAX(uint8_t par)
{
    LCD_SCLLow();
    LCD_DCXHigh();
    LCD_CSLow();

    for (uint8_t i = 0; i < 8; i++)
    {
        LCD_SCLLow();
        if (par & 0x80)
        {
            LCD_SDAHigh();
        }
        else
        {
            LCD_SDALow();
        }
        par = par << 1;
        LCD_SCLHigh();
    }
    LCD_CSHigh();
}
//**********************************read function begin********************************************
void sda_state(uint8_t state)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    if (state == 0)
    {
        GPIO_InitStruct.Pin = SPI1_MOSI_Pin;
        GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    }
    else if (state == 1)
    {
        GPIO_InitStruct.Pin = SPI1_MOSI_Pin;
        GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    }
}
uint32_t read_reg(uint8_t reg)
{
    uint32_t re_val = 0;
    uint16_t addr = reg;
    uint16_t id1, id2, id3 = 0;
    sda_state(io_writ);
    // LCD_SCLLow();
    LCD_DCXLow();
    LCD_CSLow();
    for (uint8_t i = 0; i < 8; i++)
    {
        LCD_SCLLow();
        if (reg & 0x80)
        {
            LCD_SDAHigh();
        }
        else
        {
            LCD_SDALow();
        }
        reg = reg << 1;
        LCD_SCLHigh();
    }
    LCD_DCXHigh();
    sda_state(io_read);
    for (uint8_t i = 0; i < 24; i++)
    {
        LCD_SCLLow();
        re_val = re_val << 1;
        re_val = re_val + HAL_GPIO_ReadPin(SPI1_MOSI_GPIO_Port, SPI1_MOSI_Pin);
        LCD_SCLHigh();
    }
    LCD_CSHigh();
    LCD_SCLLow();

    sda_state(io_writ);
    id1 = (re_val >> 16) & 0xFF;
    id2 = (re_val >> 8) & 0xFF;
    id3 = (re_val) & 0xFF;

    printf("reg 0x%02x data is\n\r%02x\n\r%02x\n\r%02x\n\r", addr, id1, id2, id3);

    return re_val;
}

uint16_t read_one(uint8_t reg)
{
    uint16_t re_val = 0;
    uint16_t addr = reg;
    sda_state(io_writ);
    // LCD_SCLLow();
    LCD_DCXLow();
    LCD_CSLow();
    for (uint8_t i = 0; i < 8; i++)
    {
        LCD_SCLLow();
        if (reg & 0x80)
        {
            LCD_SDAHigh();
        }
        else
        {
            LCD_SDALow();
        }
        reg = reg << 1;
        LCD_SCLHigh();
    }
    LCD_DCXHigh();
    sda_state(io_read);
    for (uint8_t i = 0; i < 8; i++)
    {
        LCD_SCLLow();
        re_val = re_val << 1;
        re_val = re_val + HAL_GPIO_ReadPin(SPI1_MOSI_GPIO_Port, SPI1_MOSI_Pin);
        LCD_SCLHigh();
    }
    LCD_CSHigh();
    LCD_SCLLow();

    printf("reg %02x one data is %02x\n\r", addr, re_val);

    sda_state(io_writ);

    return re_val;
}

void pb_out(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    // db 0-7
    GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 |
                          GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void pb_in(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    // db 0-7
    GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 |
                          GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
uint8_t i801_read_byte(void)
{
    uint8_t v;

    LCD_DCXHigh();
    LCD_RDLow();
    v = GPIOB->IDR;
    LCD_RDHigh();

    return v;
}
void rd_parall1_8(uint8_t c, uint8_t *d, uint32_t l)
{
    uint8_t buf[128];

    wr_cmd_parall1_8(c);
    pb_in();
    for (int i = 0; i < l; i++)
    {
        buf[i] = i801_read_byte();
    }
    LCD_CSHigh();
    pb_out();
    memcpy(d, buf, l);
}
//**********************************write display begin********************************************
void wr_displ_parall1_8(uint8_t color)
{
    LCD_DCXHigh();
    LCD_WRLow();
    GPIOB->ODR = color;
    LCD_WRHigh();
}
void wr_displ_parall2_18(uint16_t color1, uint16_t color2)
{
    LCD_DCXHigh();
    LCD_WRLow();
    if (color1 & 0x02)
        HAL_GPIO_WritePin(DB17_GPIO_Port, DB17_Pin, GPIO_PIN_SET);
    else
        HAL_GPIO_WritePin(DB17_GPIO_Port, DB17_Pin, GPIO_PIN_RESET);
    if (color1 & 0x01)
        HAL_GPIO_WritePin(DB16_GPIO_Port, DB16_Pin, GPIO_PIN_SET);
    else
        HAL_GPIO_WritePin(DB16_GPIO_Port, DB16_Pin, GPIO_PIN_RESET);
    GPIOB->ODR = color2;
    LCD_WRHigh();
}
// #endif
void C096LAB_Init(void)
{
    LCD_RSTHigh();
    HAL_Delay(120);
    LCD_RSTLow();
    HAL_Delay(120);
    LCD_RSTHigh();
    HAL_Delay(200);

    LCD_WR_REG(0xFF);
    LCD_WR_DATAX(0x20);
    LCD_WR_DATAX(0x10);
    LCD_WR_DATAX(0x00);


	LCD_WR_REG(0x11);     

	HAL_Delay(120);                //ms

	LCD_WR_REG(0x36);     
	LCD_WR_DATAX(0xC0);   

	LCD_WR_REG(0x3A);     
	LCD_WR_DATAX(0x66);   

	LCD_WR_REG(0xB2);     
	LCD_WR_DATAX(0x0C);   
	LCD_WR_DATAX(0x0C);   
	LCD_WR_DATAX(0x00);   
	LCD_WR_DATAX(0x33);   
	LCD_WR_DATAX(0x33);   

	LCD_WR_REG(0xB7);     
	LCD_WR_DATAX(0x35);   

	LCD_WR_REG(0xBB);     //VCOM  
	LCD_WR_DATAX(0x1E);   

	LCD_WR_REG(0xC0);     
	LCD_WR_DATAX(0x2C);   

	LCD_WR_REG(0xC2);     
	LCD_WR_DATAX(0x01);   

	LCD_WR_REG(0xC3);     //GVDD  
	LCD_WR_DATAX(0x0B);   

	LCD_WR_REG(0xC4);     
	LCD_WR_DATAX(0x20);   

	LCD_WR_REG(0xC6);     
	LCD_WR_DATAX(0x0F);   

	LCD_WR_REG(0xD0);     
	LCD_WR_DATAX(0xA4);   
	LCD_WR_DATAX(0xA1); 

	LCD_WR_REG(0xD6);     
	LCD_WR_DATAX(0xA1); 
		

	LCD_WR_REG(0xBB);     
	LCD_WR_DATAX(0x1A);   

	LCD_WR_REG(0xE0);     
	LCD_WR_DATAX(0xD0);   
	LCD_WR_DATAX(0x06);   
	LCD_WR_DATAX(0x0B);   
	LCD_WR_DATAX(0x07);   
	LCD_WR_DATAX(0x07);   
	LCD_WR_DATAX(0x24);   
	LCD_WR_DATAX(0x2E);   
	LCD_WR_DATAX(0x32);   
	LCD_WR_DATAX(0x46);   
	LCD_WR_DATAX(0x37);   
	LCD_WR_DATAX(0x13);   
	LCD_WR_DATAX(0x13);   
	LCD_WR_DATAX(0x2D);   
	LCD_WR_DATAX(0x33);   

	LCD_WR_REG(0xE1);     
	LCD_WR_DATAX(0xD0);   
	LCD_WR_DATAX(0x02);   
	LCD_WR_DATAX(0x06);   
	LCD_WR_DATAX(0x09);   
	LCD_WR_DATAX(0x08);   
	LCD_WR_DATAX(0x05);   
	LCD_WR_DATAX(0x29);   
	LCD_WR_DATAX(0x44);   
	LCD_WR_DATAX(0x42);   
	LCD_WR_DATAX(0x38);   
	LCD_WR_DATAX(0x14);   
	LCD_WR_DATAX(0x14);   
	LCD_WR_DATAX(0x2A);   
	LCD_WR_DATAX(0x30);   
	
	LCD_WR_REG(0x2A);
	LCD_WR_DATAX(0x00);
	LCD_WR_DATAX(0x23);
	LCD_WR_DATAX(0x00);
	LCD_WR_DATAX(0xCC);
	
	LCD_WR_REG(0x2B);
	LCD_WR_DATAX(0x00);
	LCD_WR_DATAX(0x00);
	LCD_WR_DATAX(0x01);
	LCD_WR_DATAX(0x3F);

	LCD_WR_REG(0x21);     

	LCD_WR_REG(0x29);     
    // wr_cmd_3spi_9bit(0x1C);
}
